/*****************************************************************************/
/* lottocheck.C v0.1.159.869                                                     */
/*****************************************************************************/
/*
###############################################################################

@purpose:	evaluating lottery results, against hardcoded lottery ticket
			numbers. Game 77 and Super 6 is evaluated along with lottery
			numbers.

@input:		Actual drawing date, actual lottery numbers, actuals Game 77 and
			actual Super 6. Data are entered during runtime (dos prompt).

@output:	result file with showing matches and winning class.

@author:	rozumek@gmx.com

@created:	12/20/2009

@last mod:	01/10/2010

###############################################################################
#
# REVISION HISTORY
#
# 0.1.159.869   01/10/2010  Initial Release
#
###############################################################################
*/

// ############################################################################
// DO NOT CHANGE ANYTHING BELOW !!!
// ############################################################################

// INCLUDE LIBRARIES & DEFINITIONS ''''''''''''''''''''''''''''''''''''''''''''

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "version.h"

#define NOLR 12			// N_umber O_f L_ottery R_ows -1 (First Row with index 0)
#define N 80
#define true 1
#define false 0

// GLOBAL VARIABLES '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

static const char THISPROG[] = "LOTTOCHECK";						// Program Name
char cTicketNo[8] = "2649458";										// Ticket Number
char cPlayers[128] = "Bodo, Jan, Marek, Nicole, Pieter, Reinhard";	// Players
struct tm ts;														// standard date type
int ALN[6];															// Actual Lottery Numbers
int ABN = 0;														// Actual Bonus Number
int ASN = -1;														// Actual Super Number
char cG77[8];														// Actual Game 77 Number
char cSU6[7];														// Actual Super 6 Number
char ResultFolder[13] = ".\\results\\";                             // Folder to store results
FILE *pFile = NULL;                                                 // Pointer to result file


char *wday[] = { "Sunday", "Monday", "Tuesday", "Wednesday",		// day of week
         "Thursday", "Friday", "Saturday", "???" };



int LottoNo[NOLR][6] = {{25,26,27,31,38,43},						// Lottery Numbers (12 rows)
						{3,16,27,30,43,49},
						{16,18,25,35,38,48},
						{9,11,17,32,37,40},
						{2,5,9,13,17,42},
						{3,10,19,33,39,47},
						{9,10,21,23,45,47},
						{4,12,16,26,44,49},
						{5,6,8,15,29,48},
						{2,5,6,8,29,33},
						{10,17,32,36,45,49},
						{15,18,27,33,37,47}};


// '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


/*****************************************************************************
 * Welcome
 ****************************************************************************/

bool welcome() {

    printf("\n%s v%d.%d.%d.%d\n",THISPROG,MAJOR,MINOR,BUILD,REVISION);
	printf("Evaluating lottery results\n") ;
	printf("Lottery Ticket No: %s\n", &cTicketNo);
}

/******************************************************************************
 * IsCorrectBonusNo
 ******************************************************************************
 prevent bonus number already exists as actual lottery number                */

 bool isCorrectBonusNo(int BN, int *LoNo) {

 	int i;
 	bool bDuplicate = false;
 	bool bValidRange = false;

 	// checks valid range ff
 	if(BN > 0 && BN < 50)
 		bValidRange = true;

 	// checks if BN already exists as acual lottery number

 	for(i = 0; i < 6; i++) {

		if(LoNo[i] == BN)
			bDuplicate = true;
	}

 	if((bValidRange == true) && (bDuplicate == false))
 		return true;

 	return false;
 }


/******************************************************************************
 * IsCorrectLotteryRow
 ******************************************************************************
 checks for valid range and duplicates                                       */

bool isCorrectLotteryRow(int *LotteryNo) {

	int i, j; int v = 0; bool bDuplicate = false;

	// checks for duplicates
	for(i = 0; i < 6; i++) {
		for(j = 0; j < 6; j++) {
			if(i != j)
				if(LotteryNo[i] == LotteryNo[j])
					bDuplicate = true;
		}
	}


	// checks for valid range
	for(i = 0; i < 6; i++) {
		if(LotteryNo[i] > 0 &&  LotteryNo[i] < 50)
			v++;
	}

	if((v == 6) && (bDuplicate == false))
		return true;

	return false;
}

/******************************************************************************
 * IsLeapYear
 *****************************************************************************/

bool isLeapYear(const int iYear)
{
  // Each year that could be devided by 4 without rest is a leap year.
  // Exception: year could be devided by 100 without rest but not by 400.

  if ((iYear % 400) == 0)
    return true;
  else if ((iYear % 100) == 0)
    return false;
  else if ((iYear % 4) == 0)
    return true;

  return false;
}


/******************************************************************************
 * IsCorrectDateFormat
 *****************************************************************************/

bool isCorrectDateFormat(int m, int d, int y) {

	int ii = 0;		// incremented by 1 for each positive validation
	int febdays;	// used for number of days depending on year

	/* Checking month '''''''''''''''''''''''''''''''''''''''''''''''''''''' */
	if(m > 0 && m < 13)
		ii++;
	else
		printf("\nInvalid value for month!\n");

	/* Checking day depending on month ''''''''''''''''''''''''''''''''''''' */

	// Jan, Mar, May, Jul, Aug, Oct, Dec
	if(m==1|| m==3 || m==5 || m==7 || m==8 || m==10 || m==12) {
		if(d > 0 && d < 32)
			ii++;
		else
			printf("\nInvalid value for day!\n");
	}

	// Feb (validating leap years)

	if((isLeapYear(y)) == true)
		febdays = 29;
	else
		febdays = 28;

	if(m==2) {
		if(d > 0 && d < (febdays + 1))
			ii++;
		else
			printf("\nInvalid value for day!\n");
	}

	// Apr, Jun, Sep, Nov
	if(m==4|| m==6 || m==9 || m==11) {
		if(d > 0 && d < 31)
			ii++;
		else
			printf("\nInvalid value for day!\n");
	}

	/* Checking year ''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
	if(y > 1 && y < 10000)
		ii++;
	else
		printf("\nInvalid value for year!\n");

	/* Evaluating complete result '''''''''''''''''''''''''''''''''''''''''' */

	if(ii == 3)
		return true;
	else
		return false;

}


/******************************************************************************
 * Convert To Digit
 ******************************************************************************
 converts single char in range of '1' to '9' to number.                       */

 /* returns int value if in range '0'..'9' else returns -1 if not a number */

int convertToDigit( char c )
{
    if ( c < '0' || c > '9' ) return -1;
    return c - '0';
}

/******************************************************************************
 * Get Lottery Win Class
 ******************************************************************************
 Function gets number of lottery matches and bonus or super number and
 returns lottery win class.                                                  */

 char *getWinClass(int matches, bool bonus_super ) {

     if(bonus_super == false) {

         switch(matches) {

             case 3: return "class VIII"; break;
             case 4: return "class VI"; break;
             case 5: return "class IV"; break;
             case 6: return "class II"; break;
             default: return "no win";
         }

     } else {

         switch(matches) {

             case 3: return "class VII"; break;
             case 4: return "class V"; break;
             case 5: return "class III"; break;
             case 6: return "class I"; break;
             default: return "no win";
         }
     }
}


/******************************************************************************
 * check Lotto
 ******************************************************************************

 Evaluates actual lottery numbers against lottery ticket
 in order to determine matches and win classes                               */

 int checkLotto() {

 	int RowNo, i, j;							// row number, indizees
 	int MPR[NOLR];								// matches per lottery row
 	int WinRows = 0;                            // number of lottery rows with win
 	bool CBN[NOLR];                             // correct bonus number
 	bool CSN[NOLR];                             // correct super number
 	char sDrwDate[N];			                // Drawing date formated
 	int iNOLR = NOLR;                           // Number of lottery rows (hard coded)
 	char WinMsg[12];                            // literal to show win class for ouput

 	// Initialize MPR, CBN and CSN
 	for(i = 0; i < NOLR; i++) {
        MPR[i] = 0; CBN[i] = false; CSN[i] = false;
 	}

// check for matches with actual lottery numbers ''''''''''''''''''''''''''''''

    for(RowNo = 0;RowNo < NOLR; RowNo++)
        for(i = 0; i < 6; i++)
            for(j = 0; j < 6; j++)
                if(LottoNo[RowNo][i] == ALN[j])
                    MPR[RowNo]++ ;

    // check for correct Super and Bonus Number '''''''''''''''''''''''''''''''

    for(RowNo = 0; RowNo < NOLR; RowNo++) {

        // check for correct Super Number
        if(MPR[RowNo] > 2) {
            WinRows++;
            if(MPR[RowNo] > 5) {
                if(ASN == convertToDigit(cTicketNo[6]))
                    CSN[RowNo] = true;
            } else {
                    for(i = 0; i < 6; i++)
                        if(LottoNo[RowNo][i] == ABN)
                            CBN[RowNo] = true;

                    }
        }
    }

    // Generate output ''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    // Console Output

    strftime(sDrwDate, N, "%A, %d-%b-%Y", &ts);
    printf("\nLottery Matches on %s\n\n", sDrwDate);

    for(RowNo = 0; RowNo < NOLR; RowNo++) {

        if(MPR[RowNo] < 6)
            strcpy(WinMsg, getWinClass(MPR[RowNo], CBN[RowNo]));
        else
            strcpy(WinMsg, getWinClass(MPR[RowNo], CSN[RowNo]));

        switch(MPR[RowNo]) {
            case 0: {   printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / no match)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg);
                        fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / no match)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg);
                    }
                    break;
            case 1: {   printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i match)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i match)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                    }
                    break;
            case 2: {   printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                    }
                    break;
            case 6: {   if(CSN[RowNo] == true) {
                            printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches + correct super number)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                            fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches + correct super number)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        } else {
                            printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                            fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        }
                    }
                    break;
            default: {  if(CBN[RowNo] == true) {
                            printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches + correct bonus number)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                            fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i matches + correct bonus number)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        } else {
                            printf("Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i Matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                            fprintf(pFile, "Row No %2i: %2i %2i %2i %2i %2i %2i\t(%s / %i Matches)\n",RowNo + 1,LottoNo[RowNo][0], LottoNo[RowNo][1],LottoNo[RowNo][2],LottoNo[RowNo][3],LottoNo[RowNo][4],LottoNo[RowNo][5],&WinMsg,MPR[RowNo]);
                        }
                    }
        }

    }

    switch(WinRows) {
        case 0: {
            printf("\nThere is no win for any of %i rows played in total.\n",iNOLR);
            fprintf(pFile, "\nThere is no win for any of %i rows played in total.\n",iNOLR);
            break;
        }
        case 1: {
            printf("\nThere is %i row with win of %i rows played in total.\n",WinRows,iNOLR);
            fprintf(pFile, "\nThere is %i row with win of %i rows played in total.\n",WinRows,iNOLR);
            break;
        }
        default: {
            printf("\nThere are %i rows with wins of %i rows played in total.\n",WinRows,iNOLR);
            fprintf(pFile, "\nThere are %i rows with wins of %i rows played in total.\n",WinRows,iNOLR);
        }
    }

    // File Output
    // printf("\nPlayers: %s\n", &cPlayers);
    // printf("%i %i %i %i %i %i\n");

 	return 0;
}



/******************************************************************************
 * Enter Input
 *****************************************************************************/

int enterInput() {

 	// Actual Play Date (drawing date) ''''''''''''''''''''''''''''''''''''''''

    int year, month, day;		// year, month, day as enterd by user
    bool is_ok = false;			// correctness of date format
    char sPlayDate[40];			// Actual Drawing Date
    int i;						// Actual Lottery Number Index
    bool first_input = true;	// indicates first attempt for input
    char sDrwDate[N];			// Drawing date formated


    do
    {
    	if(first_input == true)
    		printf("\nEnter drawing date (mm/dd/yyyy): ");
    	else
    		printf("Invalid input! Please correct: ");
    	scanf("%d/%d/%d", &month, &day, &year);
    	fflush(stdin);
    	is_ok = (isCorrectDateFormat(month, day, year));
    	first_input = false;

    } while(is_ok == false);

    ts.tm_year = year - 1900;
    ts.tm_mon  = month - 1;
    ts.tm_mday = day;

    ts.tm_hour = 0;
    ts.tm_min  = 0;
    ts.tm_sec  = 1;
    ts.tm_isdst = -1;

    if ( mktime(&ts) == -1 )
      ts.tm_wday = 7;

	strftime(sPlayDate, 40, "%A, %d-%b-%Y", &ts);

    is_ok = false;		// reset to false for next evaluation
    first_input = true;	// reset to true for next evaluation

 	// Actual Lottery Numbers '''''''''''''''''''''''''''''''''''''''''''''''''

 	do {
    	for(i = 0; i < 6; ++i)								// Initialize ALN
    		ALN[i] = 0;

 		if(first_input == true)
 			printf("Enter actual lottory numbers seperated by commas: ");
 		else
 			printf("Invalid input! Please correct: ");
 		scanf("%i,%i,%i,%i,%i,%i", &ALN[0], &ALN[1], &ALN[2], &ALN[3], &ALN[4], &ALN[5]);
 		fflush(stdin);
 		is_ok = (isCorrectLotteryRow(ALN));
 		first_input = false;

 	} while(is_ok == false);

 	first_input = true;	// reset to true for next evaluation

 	// Actual Lottery Bonus Number ''''''''''''''''''''''''''''''''''''''''''''

 	do {
 		if(first_input == true)
 			printf("Enter actual bonus number: ");
 		else
 			printf("Invalid input! Please correct: ");
 		scanf("%i", &ABN);
 		fflush(stdin);
 		first_input = false;

 	} while((isCorrectBonusNo(ABN, ALN)) == false);

 	first_input = true;	// reset to true for next evaluation

 	// Actual Lottery Super Number ''''''''''''''''''''''''''''''''''''''''''''

 	do {
 		if(first_input == true)
 			printf("Enter actual super number: ");
 		else
 			printf("Invalid input! Please correct: ");
 		scanf("%i", &ASN);
 		fflush(stdin);
 		first_input = false;

 	} while(ASN < 0 || ASN > 9);

 	first_input = true;	// reset to true for next evaluation

 	// Actual Game 77 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 	do {
 		if(first_input == true)
 			printf("Enter actual Game 77 number: ");
 		else
 			printf("Invalid input! Please correct: ");
 		scanf("%7[0123456789]", cG77);
 		fflush(stdin);
 		first_input = false;

 	} while((strlen(cG77)) < 7);

 	first_input = true;	// reset to true for next evaluation

 	// Actual Super 6 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''

 	do {
 		if(first_input == true)
 			printf("Enter actual Super 6 number: ");
 		else
 			printf("Invalid input! Please correct: ");
 		scanf("%6[0123456789]", cSU6);
 		fflush(stdin);
 		first_input = false;

 	} while((strlen(cSU6)) < 6);

 	first_input = true;	// reset to true for next evaluation

 	// Console Output

    strftime(sDrwDate, N, "%A, %d-%b-%Y", &ts);

    printf("\nCheck your input:\n");
    printf("\nDrawing Date: %s\n", sDrwDate);
 	printf("Actual lottery numbers: %i %i %i %i %i %i\n",ALN[0],ALN[1],ALN[2],ALN[3],ALN[4],ALN[5]);
 	printf("Actual bonus number: %i\n", ABN);
 	printf("Actual super number: %i\n", ASN);
 	printf("Actual Game 77: %s\n", &cG77);
 	printf("Actual Super 6: %s\n", &cSU6);

 	return 0;

}

/******************************************************************************
 * check Game 77
 ******************************************************************************

 Evaluates actual lottery numbers against lottery ticket
 in order to determine matches and win classes                               */

 int checkGame77() {

 	int ii;									    // index of ticket number array
 	int MatchG77 = 0;							// matches Game 77
 	char WinClassG77[4];                        // Game 77 win class

    for(ii = 6; ii >= 0; ii--) {
        if(cTicketNo[ii] == cG77[ii])
            MatchG77++;
        else
            break;
    }

    if(MatchG77 > 0) {
        switch(MatchG77) {

            case 1: strcpy(WinClassG77, "VII"); break;
            case 2: strcpy(WinClassG77, "VI"); break;
            case 3: strcpy(WinClassG77, "V"); break;
            case 4: strcpy(WinClassG77, "IV"); break;
            case 5: strcpy(WinClassG77, "III"); break;
            case 6: strcpy(WinClassG77, "II"); break;
            case 7: strcpy(WinClassG77, "I"); break;
            default: strcpy(WinClassG77, "---");
        }

        if(MatchG77 == 1) {
            printf("You have won Game 77 according winning class %s (%i digit matching).\n",&WinClassG77,MatchG77);
            fprintf(pFile, "You have won Game 77 according winning class %s (%i digit matching).\n",&WinClassG77,MatchG77);
        }

        if(MatchG77 > 1) {
            printf("You have won Game 77 according winning class %s (%i digits matching).\n",&WinClassG77,MatchG77);
            fprintf(pFile, "You have won Game 77 according winning class %s (%i digits matching).\n",&WinClassG77,MatchG77);
        }

    } else {
        printf("There is no win for Game 77.\n");
        fprintf(pFile, "There is no win for Game 77.\n");

    }

 	return 0;
}

/******************************************************************************
 * check Super 6
 ******************************************************************************

 Evaluates actual lottery numbers against lottery ticket
 in order to determine matches and win classes                               */

 int checkSuper6() {


 	int ii, jj;									// index of ticket number array
 	int MatchSU6 = 0;							// matches Super 6
 	char WinClassSU6[4];                        // Game 77 win class

    for(ii = 6; ii >= 0; ii--) {
        if(cTicketNo[ii] == cSU6[ii -1])
            MatchSU6++;
        else
            break;
    }

    if(MatchSU6 > 0) {
        switch(MatchSU6) {

            case 1: strcpy(WinClassSU6, "VI"); break;
            case 2: strcpy(WinClassSU6, "V"); break;
            case 3: strcpy(WinClassSU6, "IV"); break;
            case 4: strcpy(WinClassSU6, "III"); break;
            case 5: strcpy(WinClassSU6, "II"); break;
            case 6: strcpy(WinClassSU6, "I"); break;
            default: strcpy(WinClassSU6, "---");
        }


        if(MatchSU6 == 1) {
            printf("You have won Super 6 according winning class %s (%i digit matching).\n",&WinClassSU6,MatchSU6);
            fprintf(pFile, "You have won Super 6 according winning class %s (%i digit matching).\n",&WinClassSU6,MatchSU6);
        }

        if(MatchSU6 > 1) {
            printf("You have won Super 6 according winning class %s (%i digits matching).\n",&WinClassSU6,MatchSU6);
            fprintf(pFile, "You have won Super 6 according winning class %s (%i digits matching).\n",&WinClassSU6,MatchSU6);

        }

    } else {
        printf("There is no win for Super 6.\n");
        fprintf(pFile, "There is no win for Super 6.\n");

    }

 	return 0;
}


/*****************************************************************************
 * Main
 ****************************************************************************/

int main() {

	bool isFirstLoop = true;			        // indicates first main prgramm loop
	bool goExit = false;				        // indicates not to leave main loop
	int sConfirm;						        // yes or no to confirm user input
	char sPath[45];                             // Full pathname for result file
	char sPrefix[14] = "Lotto-Result-";         // File prefix
	char sPostfix[5] = ".txt";                  // File extension
	char sDrwDate[25];                          // Drawing Date formatted as part of filename
	char WindowTitle[25];                       // Title of console window

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // Console handle


	//Set console attributes

    strcpy(WindowTitle, "JCASGBUR :: ");
    strcat(WindowTitle, THISPROG);
	SetConsoleTitle(WindowTitle);
	SetConsoleTextAttribute(hConsole, 10);

	welcome();

	do {

		enterInput();
		printf("\nEvaluate results now? [y/n]: ");

		do {
			sConfirm = tolower(getchar());
		} while(sConfirm != 'y' && sConfirm != 'n');

		if(sConfirm == 'y') {

			// Create Filename
            strftime(sDrwDate, N, "%Y-%m-%d", &ts); strcpy(sPath, ResultFolder);
			strcat(sPath, sPrefix); strcat(sPath, sDrwDate); strcat(sPath, sPostfix);

            // Open result file for output
            pFile = fopen(sPath, "w");
            if(pFile == NULL) {
                printf("\nResult folder missing. Try to create now...\n", sPath);
                system("mkdir results");
                pFile = fopen(sPath, "w");
                if(pFile == NULL) {
                    printf("Error opening %s for writing. Program terminated.", sPath);
                    abort();
                } else {
                    printf("Folder \"%s\" has been created.\n", ResultFolder);
                }
            }

            // Output to result file first part (header information)
            fprintf(pFile, "%s v%d.%d.%d.%d\n", THISPROG,MAJOR,MINOR,BUILD,REVISION);
            fprintf(pFile, "Evaluating lottery results\n");
            fprintf(pFile, "Lottery Ticket No: %s\n", &cTicketNo);

            fprintf(pFile, "\nPlayers: %s\n", &cPlayers);

            strftime(sDrwDate, N, "%A, %d-%b-%Y", &ts);

            fprintf(pFile, "\nDrawing Date: %s\n", sDrwDate);
            fprintf(pFile, "Lottery numbers: %i %i %i %i %i %i\n",ALN[0],ALN[1],ALN[2],ALN[3],ALN[4],ALN[5]);
            fprintf(pFile, "Bonus number: %i\n", ABN);
            fprintf(pFile, "Super number: %i\n", ASN);
            fprintf(pFile, "Game 77: %s\n", &cG77);
            fprintf(pFile, "Super 6: %s\n", &cSU6);

            fprintf(pFile, "\nLottery Matches on %s\n\n", sDrwDate);

            // Evaluate results
			checkLotto(); checkGame77(); checkSuper6();
			printf("\nWithout any warranty.\n");

			// Final output for result file
			fprintf(pFile, "\nWithout any warranty.\n");

			 // Close result file
            fclose(pFile);
            printf("\nResults written to %s.\n", sPath);

            // New loop ?
            printf("\nEnter new drawing results ? [y/n]: ");
            do {
                sConfirm = tolower(getchar());
            } while(sConfirm != 'y' && sConfirm != 'n');

            if(sConfirm == 'n')
                goExit = true;
		}

	} while(goExit == false);
}

